package com.yz.lzq.liwushuo.activity;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.fragment.CategoryFragment;
import com.yz.lzq.liwushuo.fragment.HomeFragment;
import com.yz.lzq.liwushuo.fragment.ProfileFragment;
import com.yz.lzq.liwushuo.fragment.gift.GiftFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.yz.lzq.liwushuo.R.id.radioGroup_main;


public class MainActivity extends AppCompatActivity {
    @Bind(R.id.frameLyout_main)
    FrameLayout frameLyoutMain;
    @Bind(radioGroup_main)
    RadioGroup radioGroupMain;
    //底部标签导航用到的RadioButton
    private RadioButton[] mArrRadioButtons = null;
    private Context mContext = this;
    //fragment的数据源
    private List<Fragment> mList_Fragment = new ArrayList<>();
    //显示主页面的FrameLayout
    private String[] mArrTabtitle = null;
    private TypedArray typedArray;
    private Drawable[] mDrawable = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        initData();
        initTabs();

    }

    /**
     * 初始化数据
     */
    private void initData() {
        mArrTabtitle = getResources().getStringArray(R.array.arrTabTitles);
        typedArray = getResources().obtainTypedArray(R.array.arrIconDrawable);
        mDrawable = new Drawable[typedArray.length()];
        for (int i = 0; i < mArrTabtitle.length; i++) {
            mDrawable[i] = typedArray.getDrawable(i);
        }
        HomeFragment homeFragment = HomeFragment.getInstance();
        GiftFragment giftFragment = GiftFragment.getInstance();
        CategoryFragment categoryFragment = CategoryFragment.getInstance();
        ProfileFragment profileFragment = ProfileFragment.getInstance();
        mList_Fragment.add(homeFragment);
        mList_Fragment.add(giftFragment);
        mList_Fragment.add(categoryFragment);
        mList_Fragment.add(profileFragment);

    }

    /**
     * 初始化底部标签导航
     */
    private void initTabs() {
        //文字的普通颜色
        final int otherColor = getResources().getColor(R.color.dimgray);
        //文字被选中的颜色
        final int selecttedColor = getResources().getColor(R.color.deeppink);
        //初始化radioButton数组
        mArrRadioButtons = new RadioButton[mArrTabtitle.length];
        //获取屏幕的宽度
        int screenWidthPx = getResources().getDisplayMetrics().widthPixels;
        //循环设置RadioButton
        for (int i = 0; i < mArrTabtitle.length; i++) {
            //初始化
            mArrRadioButtons[i] = new RadioButton(mContext);
            //设置文字
            mArrRadioButtons[i].setText(mArrTabtitle[i]);
            mArrRadioButtons[i].setButtonDrawable(android.R.color.transparent);
            mArrRadioButtons[i].setBackgroundResource(android.R.color.transparent);
            mDrawable[i].setBounds(0, 0, mDrawable[i].getIntrinsicWidth(), mDrawable[i]
                    .getIntrinsicHeight());
            mArrRadioButtons[i].setCompoundDrawables(null, mDrawable[i], null, null);
            mArrRadioButtons[i].setGravity(Gravity.CENTER);
            mArrRadioButtons[i].setWidth(screenWidthPx / mArrTabtitle.length);
            mArrRadioButtons[i].setTextColor(otherColor);
            radioGroupMain.addView(mArrRadioButtons[i]);
        }
        mArrRadioButtons[0].setTextColor(selecttedColor);
        mArrRadioButtons[0].setChecked(true);
        switchFragment(getSupportFragmentManager(), mList_Fragment, 0, R.id.frameLyout_main);
        radioGroupMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < mArrRadioButtons.length; i++) {
                    mArrRadioButtons[i].setTextColor(otherColor);
                    if (mArrRadioButtons[i].getId() == checkedId) {
                        mArrRadioButtons[i].setTextColor(selecttedColor);
                        //动态生成碎片
                        switchFragment(getSupportFragmentManager(), mList_Fragment, i, R.id
                                .frameLyout_main);
                    }

                }
            }
        });
    }

    /**
     * @param manager
     * @param list
     * @param tabIndex
     * @param layoutId 选择显示的fragment
     */
    public void switchFragment(FragmentManager manager, List<Fragment> list, int tabIndex, int
            layoutId) {
        FragmentTransaction transaction = manager.beginTransaction();
        //让当前显示的碎片进行隐藏
        int currentTabIndex = 0;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).isVisible()) {
                transaction.hide(list.get(i));
            }
        }
     /*   //设置碎片显示的自定义动画
        if (enter != 0 && exit != 0) {
            transaction.setCustomAnimations(enter, exit);
        }*/
        Fragment toFragment = list.get(tabIndex);
        if (toFragment.isAdded()) {
            transaction.show(toFragment);
        } else {
            transaction.add(layoutId, toFragment);
        }
        transaction.commit();
    }
}
